package com.rnkrsoft.suyan.config.server.web.controller;

import com.rnkrsoft.suyan.config.server.ajax.config.AjaxConfig;
import com.rnkrsoft.suyan.config.server.ajax.executor.AjaxExecutor;
import com.rnkrsoft.suyan.config.server.ajax.executor.AjaxExecutorFactory;
import com.rnkrsoft.suyan.config.server.ajax.executor.ExecutorType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.doc._3rd.A;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class AjaxController implements InitializingBean {


    private AjaxExecutor ajaxExecutor;

    @RequestMapping(value = "/ajax", method = {RequestMethod.POST})
    public void post(HttpServletRequest request, HttpServletResponse response){
        ajaxExecutor.execute(request, response);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        AjaxConfig ajaxConfig = AjaxConfig.builder()
                .executeType(ExecutorType.SPRING.name())
                .authorityExtractorName("mock")
                .authorityAuthenticatorName("mock")
                .serviceBasePackage("com.rnkrsoft.suyan.config.server.services")
                .build();
        this.ajaxExecutor = AjaxExecutorFactory.getInstance(ajaxConfig);
    }
}
